******************************************************************************************
*MERGING WAVE 1 INIANA OVERSAMPLE WITH WAVE 1 NATIONAL SAMPLE AND COMBINING WAVES 1 AND 2
******************************************************************************************
clear all



**to string Indiana caseid
use CLEAN_wave1_Indiana.dta, clear
tostring caseid*, replace
drop _m
save CLEAN_wave1_Indiana.dta, replace

clear

**Merge national wave 1 with Indiana wave 1
use CLEAN_wave1_National.dta, clear
drop _m
tostring caseid*, replace
sort caseid
merge 1:1 caseid using CLEAN_wave1_Indiana.dta
drop _m
save tmp1, replace

**Merge in updated weights for National sample + Indiana oversample
import delimited "weights_national_extra_cmd-2.csv", clear

tostring caseid*, replace

save "weights_national_extra_cmd-2.dta", replace

use tmp1, clear

merge 1:1 caseid using weights_national_extra_cmd-2.dta, nogenerate

drop weight1
rename weight weight1

save tmp1, replace

** Pull in wave 2 national data
use CLEAN_wave2.dta, clear

tostring caseid*, replace
gen newid=caseid_National if caseid_National!="."
order newid
replace newid=caseid_Indiana if caseid_Indiana!="."

rename caseid caseid_w2
rename newid caseid
sort caseid

** Merge back in wave 1 national data
merge 1:1 caseid using tmp1.dta
order caseid*
drop _m

rename caseid caseid_w1
sort caseid_w1

drop if weight1==.

save COMBINED_waves_12.dta, replace

clear
erase tmp1.dta


*************************************
*COMBINING WAVES 1 AND 2 WITH WAVE 3
*************************************
clear all

use CLEAN_wave3.dta, clear

tostring caseid*, replace

drop _m
sort caseid_w1
merge m:1 caseid_w1 using COMBINED_waves_12.dta
tab _merge
drop if _merge==1
drop _m

rename caseid caseid_w3
sort caseid_w1
save COMBINED_waves_123.dta, replace
clear

****************************************
*COMBINING WAVES 1, 2, AND 3 WITH WAVE 4
****************************************
clear all

use CLEAN_wave4.dta, clear
tostring caseid*, replace

drop _m
sort caseid_w1
merge m:1 caseid_w1 using COMBINED_waves_123.dta
tab _merge
drop if _merge==1
drop _m

rename caseid caseid_w4

order caseid_w1 caseid_w2 caseid_w3 caseid_w4
save COMBINED_waves_1234.dta, replace
clear